
// Micro Contents: Star status and tags

function returnStar (threadID) {
	var data = gData.currThreads[threadID];
	var text = '<a style="text-decoration : none;"'
	text += ' href="javascript:toggleStar(' + threadID + ')">';
	text += '<img border="0" src="img/star_';
	if (data.isStar)
		text += 'active.png';
	else
		text += 'inactive.png';	
	text += '"></a>';
	
	return text;
}

function toggleStar (threadID) {
	var data = gData.currThreads[threadID];

	if (data.isStar) {
		callJSON ('/ui/commit/clearStar/' + threadID, dumbFunction);
		data.isStar = false;
	} else {
		callJSON ('/ui/commit/setStar/' + threadID, dumbFunction);
		data.isStar = true;
	}

	renderMessageTitle(threadID);
}


// ===========================================
// Tags Edit

function createTagsArea (widget, docID, tags) {
	var el = Widget ('div',
			 'micro.tags.' + docID);
	el.tagsData = tags;
	doDrawTagsArea(el, docID);
	widget.appendChild(el);
}

function getTags (tagList, deli) {
	var first = true;
	var text = '';
	
	for (i in tagList) {
		if (tagList[i] == '-') continue;
		if (first)
			first = false;
		else
			text += deli
		text += tagList[i];
	}
	return text;
}

function doDrawTagsArea (el, docID) {
	var text = '<b>Tags: </b>';

	text += '<a class="microTags" href="javascript:drawTagsEditArea(' +
		docID + ')">';
	text += getTags(el.tagsData, ', ');	
	text += '</a>';
	el.innerHTML = text;	
}

function drawTagsArea (docID) {
	var el = $('micro.tags.' + docID);
	doDrawTagsArea(el, docID);
}

function drawTagsEditArea (docID) {
	var el = $('micro.tags.' + docID);
	var form = Widget('form');
	
	form.action = 'javascript:doModifyTags(' + docID +
		',$("micro.tagEdit").value)';
	form.innerHTML = '<b>Edit Tags:</b> ';	

	var textIn = Widget('input');
	textIn.id = 'micro.tagEdit';
	textIn.value = getTags(el.tagsData, ' ');
	form.appendChild(textIn);

	el.innerHTML = '';
	el.appendChild(form);
	textIn.focus();
	
}

function doModifyTags (docID, value) {
	var el = $('micro.tags.' + docID);
	newTagList = ('- ' + value).split(/\s+/);
	el.tagsData = newTagList;

	// Commit to server
	callJSON('/ui/commit/modifyTag/' + docID + '/' +
		 getTags(newTagList, ' '),
		 dumbFunction);
	
	// Redraw
	doDrawTagsArea(el, docID);
}


function drawThreadFilter (threadID, value) {
	var widget = $('filter.' + threadID);
	var text = '<a href="javascript:drawThreadFilterEdit(' +
		    threadID + ')">See: ';
	widget.filterValue = value;	
	if (widget.filterValue == '')
		text += 'All';
	else
		text += widget.filterValue;
	text += '</a>';

	widget.innerHTML = text;
}

function drawThreadFilterEdit (threadID) {
	var widget = $('filter.' + threadID);

	var control = '<form action="javascript:refreshThreadFilter('+
		threadID + ')">';
	control += 'See: <input type="text" id="thread.filter.edit.' +
		threadID + '" value="' + widget.filterValue + '"' +
		'size="10"></form>';
	
	widget.innerHTML = control;
	$('thread.filter.edit.' + threadID).focus()
}

function refreshThreadFilter (threadID) {
	var widget = $('filter.' + threadID);
	var value = $('thread.filter.edit.' + threadID).value.strip();	
	drawThreadFilter(threadID, value);

	if (value == '')
		doFilterThread(threadID, '-');
	else
		doFilterThread(threadID, value);	

}

function doFilterThread (threadID, value) {
	docList = gData.currThreads[threadID].docList
	for (var i in docList) {
		var content = $('content.' + docList[i]);
		var tags = $('micro.tags.' + docList[i]).tagsData;
		var match = false;
		for (var j in tags) {
			if (tags[j] == value) {
				match = true;
				break;
			}
		}
		if (match)
			content.style.display = 'block';
		else
			content.style.display = 'none';
	}
}
